﻿using System;
using System.Collections;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Xml.Linq;
using INTEX.DataBase;
using WPFExcelReport;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System.Windows.Controls;
using System.Windows;
namespace WPFExcelReport
{
    public partial class YCAAUTOEK2INTEX : BaseFunction
    {
        public DataSet SetData = new DataSet();
        public DataSet SetDispo = new DataSet();
        public DateTime tomorrow = DateTime.Now.AddDays(1);
        public DateTime yesterday = DateTime.Now.AddDays(-1);
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "ITXVVVK ", ITXDATE = "20120723", ITXVKKRS = "C0", ITXVKLFD = "100227";

        public string today = "";
        public string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public string Left(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str + "                                                                                ";
                str = str.Substring(0, num);
            }
            catch (Exception ex)
            {

            }
            return str;
        }
        OracleCommand or1;
        public OracleConnection OracleDB;
        INTEXDBC INTEXDBC;
        public string ApplicationPath
        {
            get { return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath); }
        }
        public YCAAUTOEK2INTEX(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {

            OracleDB = _OracleDB;
            proxy = _proxy;
        }
        /// <summary>
        /// 通过客户0实现不同的组合剪板条件设定。
        /// </summary>
        /// <param name="xdoc"></param>
        public bool Execute(XDocument xdoc = null)
        {
            bool flagValidate = false;
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            DateTime now = DateTime.Now;
            DataRow rt = null;
            today = now.Year.ToString() + Right("00" + now.Month.ToString(), 2) + Right("00" + now.Day.ToString(), 2);
            ITXDATE = today;
            #region============EKKOPF=====================
            if (SetData.Tables.Contains("EKKOPF"))
            {
                foreach (DataRow row in SetData.Tables["EKKOPF"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("开始创建EKKOPF:" + row["EKKAUFKRS"].ToString() + "_" + row["EKKAUFLFD"].ToString(), StatusEventType.RunningProgress);
                    INTEXDBC.SaveEKKOPF(row);
                }
            }
            #endregion
            string client = "", krs = "", lfd = "", pos = "";
            #region============EKPOS======================
            if (SetData.Tables.Contains("EKPOS"))
            {
                foreach (DataRow row in SetData.Tables["EKPOS"].Rows)
                {

                    EventDispatcher.Instance.RaiseStatus("开始创建EKPOS:" + row["EKPAUFKRS"].ToString() + "_" + row["EKPAUFLFD"].ToString() + "_" + row["EKPAUFPOS"].ToString(), StatusEventType.RunningProgress);
                    client = row["EKPMANDANT"].ToString();
                    krs = row["EKPAUFKRS"].ToString();
                    lfd = row["EKPAUFLFD"].ToString();
                    pos = row["EKPAUFPOS"].ToString();
                    INTEXDBC.SaveEKPOS(row);
                }
            }
            #endregion
            return true;
        }

        public void ReSet(DataSet setData, DataSet setDispo)
        {
            try
            {
                this.SetData = setData;
                this.SetDispo = setDispo;
                UserID = "INTEX   ";
                if (!this.SetData.Tables.Contains("INTEXMAP"))
                    return;
                if (this.SetData.Tables["INTEXMAP"].Rows.Count == 0)
                    return;
                DataRow dwIntex = this.SetData.Tables["INTEXMAP"].Rows[0];
                ITXMANDANT = dwIntex["ITXMANDANT"].ToString();
                ITXVKKRS = dwIntex["ITXVKKRS"].ToString();
                ITXVKLFD = dwIntex["ITXVKLFD"].ToString();
                ITXUSER = dwIntex["ITXUSER"].ToString();
                ITXDATE = dwIntex["ITXDATE"].ToString();
                ITXDBVERSION = dwIntex["ITXDBVERSION"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete(string client, string krs, string lfd, string pos = "000")
        {
            try
            {
                if (INTEXDBC == null)
                {
                    INTEXDBC = new INTEXDBC(OracleDB, proxy);
                }
                DataSet setResult = INTEXDBC.QueryEKPOS(client, krs, lfd);
                if (!setResult.Tables.Contains("EKPOS"))
                    return;
                int count = setResult.Tables["EKPOS"].Rows.Count;
                int cdel = 0;

                var query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            select row;
                if (pos == "000")
                {
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                        string EKPBEKZ = row["EKPBEKZ"].ToString();
                        string EKPKNTRNRL = row["EKPKNTRNRL"].ToString();
                        if (EKPBEKZ.Trim() != "")
                        {
                            MessageBox.Show("采购单数量已经确认，不能删除!");                            
                                return;                            
                        }
                        if (EKPKNTRNRL.Trim() != "")
                        {
                            MessageBox.Show("采购单已经审核，不能删除!");
                            return;
                        }
                        if (EKPME3 > 0)
                        {
                           MessageBox.Show("该采购单已经入库，不能删除!");
                                return;                            
                        }
                    }
                }
                if (pos != "000")
                {
                    query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            where row["EKPAUFPOS"].ToString() == pos
                            select row;
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());

                        if (EKPME3 > 0)
                        {
                            if (MessageBox.Show("需要继续单行删除，请确认", "该采购单已经入库，不能删除!", MessageBoxButton.YesNo) == MessageBoxResult.No)
                            {
                                return;
                            }
                        }
                    }
                }
                foreach (DataRow row in setResult.Tables["EKPOS"].Rows)
                {

                    string posh = row["EKPAUFPOS"].ToString();
                    if (pos != "000")
                    {
                        if (posh != pos)
                            continue;
                    }
                    decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                    if (EKPME3 > 0)
                    {
                        MessageBox.Show("该采购单已经入库，不能删除!");
                        continue;
                    }
                    INTEXDBC.DeleteEKPOS(client, krs, lfd, posh);
                    cdel++;
                    INTEXDBC.UpdateICC_ABAUFM("EK", client, krs, lfd, posh);
                }
                if (cdel == count)
                {
                    INTEXDBC.DeleteEKKOPF(client, krs, lfd);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}